package se.hrbustrc.hlgshopping.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import se.hrbustrc.hlgshopping.entity.Prosku;
import se.hrbustrc.hlgshopping.entity.Product;
import se.hrbustrc.hlgshopping.service.ProskuService;
import se.hrbustrc.hlgshopping.service.ProductService;
import se.hrbustrc.hlgshopping.utils.Result;

import java.util.List;

@RestController
@RequestMapping("/sku")
public class ProSkuController {

    @Autowired
    private ProskuService proskuService;
    
    @Autowired
    private ProductService productService;

    /**
     * 获取SKU列表
     */
    @GetMapping("/list")
    public Result listSku() {
        try {
            List<Prosku> skuList = proskuService.list();
            return new Result().ok(skuList);
        } catch (Exception e) {
            return new Result().error("获取SKU列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据ID获取SKU详情
     */
    @GetMapping("/{id}")
    public Result getSkuDetail(@PathVariable Integer id) {
        if (id == null) {
            return new Result().error("SKU ID不能为空");
        }

        try {
            Prosku sku = proskuService.getById(id);
            if (sku == null) {
                return new Result().error("未找到指定SKU");
            }
            return new Result().ok(sku);
        } catch (Exception e) {
            return new Result().error("获取SKU详情失败: " + e.getMessage());
        }
    }

    /**
     * 根据商品ID获取SKU列表
     */
    @GetMapping("/product/{productId}")
    public Result getSkuByProductId(@PathVariable Integer productId) {
        if (productId == null) {
            return new Result().error("商品ID不能为空");
        }

        try {
            // 假设Prosku实体中有productId字段
            // 可以根据实际字段名调整查询条件
            // 这里使用MyBatis Plus的QueryWrapper进行查询
            QueryWrapper<Prosku> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("fPID", productId); // 根据实际字段名调整
            List<Prosku> skuList = proskuService.list(queryWrapper);
            return new Result().ok(skuList);
        } catch (Exception e) {
            return new Result().error("获取商品SKU列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 根据SKU ID获取商品信息
     */
    @GetMapping("/{skuId}/product")
    public Result getProductBySkuId(@PathVariable Long skuId) {
        if (skuId == null) {
            return new Result().error("SKU ID不能为空");
        }

        try {
            // 先获取SKU信息
            Prosku sku = proskuService.getById(skuId);
            if (sku == null) {
                return new Result().error("未找到指定SKU");
            }
            
            // 通过SKU中的商品ID获取商品信息
            Long productId = sku.getPid();
            Product product = productService.getById(productId);
            if (product == null) {
                return new Result().error("未找到对应的商品信息");
            }
            
            return new Result().ok(product);
        } catch (Exception e) {
            return new Result().error("获取商品信息失败: " + e.getMessage());
        }
    }
}
